<template>
	<div class="goods w944">
		<alert ref="alert"></alert>
		<deleteAlert ref="deleteAlert"></deleteAlert>
		<div class="label_top">
			<p><i class="line"></i>商城管理</p>
		</div>
		<div class="label_table">
			<div class="el-tabs-report">
				<a class="listOne" @click="addGoods" v-show="this.nowTypeCd==''?true:false" v-if="goods.spgl_add">添加商品</a>
				<a class="listOne" @click="addSave" v-show="this.nowTypeCd=='addTj'?true:false">保存</a>
				<a class="listTwo" @click="importDaoChu" v-if="goods.spgl_export">导出数据</a>
				<div class="msearch">
					<input placeholder="请输入商品名、商品编号……" v-model="search"/>
					<a @click="getTableList"><img src="../../assets/qudaoImg/search.png"/></a>
				</div>
			</div>
			<el-table ref="multipleTable" :data="tableData" border @selection-change="handleSelectionChange">
				<el-table-column type="selection"></el-table-column>
				<el-table-column prop="itemNo" label="商品编号" width="130"></el-table-column>
				<el-table-column prop="itemName" label="商品名称"></el-table-column>
				<el-table-column prop="itemStock" label="商品数量" width="80"></el-table-column>
				<el-table-column prop="tradeNum" label="已兑换数量" width="95"></el-table-column>
				<el-table-column prop="createTimeString" label="发布时间" width="150"></el-table-column>
				<el-table-column label="商品状态" width="80">
					<template slot-scope="scope">
						<span v-if="scope.row.activityisover==0">进行中</span>
						<span v-if="scope.row.activityisover==1">已结束</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="120">
					<template slot-scope="scope">
						<div class="status" v-if="scope.row.activityisover==0">
							<el-button @click="handleEdit(scope.row)" v-if="goods.spgl_edit">编辑</el-button>
						    <el-button @click="handleDelete(scope.row)" v-if="goods.spgl_del" class="red">删除</el-button>
						</div>
						<el-button @click="handleDelete(scope.row)" v-if="scope.row.activityisover==1 && goods.spgl_del" class="red">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="fenyeMargin">
				<el-button @click="toggleSelection(tableData)" class="allSelect">全选</el-button>
				<el-button @click="handleDeleteAll()" class="allDelete" v-if="goods.spgl_delAll">批量删除</el-button>
				<div class="pag_block">
					<el-pagination @current-change="handleCurrentChange" :current-page="currentpage" :page-size="pagesize" layout="prev, pager, next" :total="total" ></el-pagination>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	import alert from '../../components/Alert.vue';
	import deleteAlert from '../../components/deleteAlert.vue';
	export default {
		data() {
			return {
				goods:'',
				search: '',
				tableData: [],
		        pagesize: 15,
		        currentpage: 1,
		        total: 1,
				multipleSelection: [],
				multipleSelectionDelete: [],
				value:'',
				courseType: "",
				learnStatus: "",
				companyid:'',
				nowTypeCd:'',
				deleteType:'delete'
			}
		},
		methods: {
			getParams() {
				var routerParams = this.$route.params.type;
				if(routerParams == 'addTj') {
					this.nowTypeCd = 'addTj';
				} else {
					this.nowTypeCd = '';
				}
			},
			getTableList(){
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/shopItem/shopItemList",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data:{
						page: this.currentpage,
						num: this.pagesize,
						fuzzyFields: this.search,
						companyId:1
					},
					success: res => {
						if(res.data.code == 200) {
							this.tableData = res.data.data.list;
							this.total=res.data.data.pageParam.items;
						}
					}
				});
			},
			handleDelete(row){
				this.transitionRow = row;
				this.$rootFunction().rootDelete();
			},
			handleDeleteAll() {
				this.$rootFunction().rootDeleteAll();
			},
			deleteSubmit() {
				if(this.deleteType == 'delete'){
					var list = [];
					list.push({
						id:this.transitionRow.id
					});
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/shopItem/deleteShopItem",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'DELETE',
						data:list,
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}else{
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/shopItem/deleteShopItem",
						data:this.multipleSelectionDelete,
						method: 'DELETE',
						headers:{'ContentType': 'application/json; charset=utf-8'},
						success: res => {
							if(res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}
			},
			handleEdit(row) {
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/shopItem/shopItemDetails",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: {id: row.id},
					success: res => {
						if(res.data.code == 200) {
							this.$router.push({
								name: 'addGoods',
								params: {
									type: 'edit'
								}
							})
							this.$root.goodsMsg = res.data.data;
						}
					}
				});
			},
			addSave(){
				let s={
					ids:this.multipleSelection.join(','),
					companyid:this.$root.userInfoMsg.companyid
				};
				this.$ajax({
					url: this.$root.jifuUrl + "/addCompanyRecommendShopItem",
					method: 'POST',
					data: this.qs.stringify(s),
					success: res => {
						if(res.data.code == 200) {
							this.$refs.alert.show({alertText: '保存成功'});
							setTimeout(() =>{
								this.$router.push({
									name:'recommend'
								});
							},2000)
						}else{
							this.failText = res.data.message;
							this.$rootFunction().deleteResFail();
						}
					}
				});
			},
			toggleSelection(rows) {
				if(rows) {
					rows.forEach(row => {
						this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect)
					})
					this.allSelect = !this.allSelect;
				}
			},
			handleSelectionChange(val) {
				this.multipleSelection = [];
				this.multipleSelectionDelete = [];
				for(var i = 0;i<val.length;i++){
					var s=val[i].id
					var deleteArr ={"id":val[i].id}
					this.multipleSelection.push(s);
					this.multipleSelectionDelete.push(deleteArr)
				}
			},
			addGoods() {
				this.$router.push({
					path: '/addGoods'
				})
			},
			handleCurrentChange(value) {
				this.currentpage = value;
				this.getTableList();
			},
			importDaoChu(){}
		},
		components: {
			alert,
			deleteAlert
		},
		mounted: function() {
			this.getParams();
		    //刷新后从session取数据
		    this.$store.commit('GET_USERINFOMSG');
            var s=this.$store.state.userInfoMsg;
            this.companyid=s.companyid;   
            this.$root.userInfoMsg=this.$store.state.userInfoMsg;
			if(this.companyid!=null){
				this.getTableList();
			}
			this.goods = this.$root.huasan.goods;
		}
	}
</script>